package com.fram.service.api.v1.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fram.bean.api.distributor.request.DistributorFarmRequest;
import com.fram.bean.api.distributor.request.DistributorUpdateRequest;
import com.fram.bean.api.distributor.response.DistributorApplyResponse;
import com.fram.bean.api.distributor.response.DistributorFarmResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.BeanMapper;
import com.fram.entity.DistributorFarm;
import com.fram.mapper.DistributorFarmMapper;
import com.fram.service.api.v1.DistributorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @作者： 陳曉鬆 @时间： 2020年5月27日 - 上午9:40:19 @description：->
 */
@Service("distributorService")
public class DistributorServiceImpl implements DistributorService {

	@Autowired
	private DistributorFarmMapper distributorFarmMapper;

	/**
	 * 1.根据经纬度 查询店铺 （商品 分类） 2.指定 id 查询店铺（商品 分类）
	 */
	@Override
	public DistributorFarmResponse selectDistributorFarm(DistributorFarmRequest bean) throws ProException {
		if (StringUtils.isEmpty(bean.getLatitude()) || StringUtils.isEmpty(bean.getLongitude())) {
			throw new ProException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		PageHelper.startPage(1, 1);
		List<DistributorFarmResponse> distributorFarmResponse = distributorFarmMapper.select(bean);
		if (distributorFarmResponse == null || distributorFarmResponse.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		if (distributorFarmResponse.get(0).getAverage() != null) {
			distributorFarmResponse.get(0).setAverage(new BigDecimal(distributorFarmResponse.get(0).getAverage().toString()).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue());
		} else {
			distributorFarmResponse.get(0).setAverage(5f);
		}
		return distributorFarmResponse.get(0);
	}

	@Override
	public PageInfo<DistributorFarmResponse> list(PageBean<DistributorFarmRequest> request) throws ProException {
		if (StringUtils.isEmpty(request.getParam().getLatitude()) || StringUtils.isEmpty(request.getParam().getLongitude())) {
			throw new ProException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<DistributorFarmResponse> distributorFarmResponse = distributorFarmMapper.select(request.getParam());
		if (distributorFarmResponse == null || distributorFarmResponse.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		for (DistributorFarmResponse d : distributorFarmResponse) {
			if (d.getAverage() != null) {
				d.setAverage(new BigDecimal(d.getAverage().toString()).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue());
			} else {
				d.setAverage(5f);
			}
		}
		return new PageInfo<>(distributorFarmResponse);
	}

	@Override
	public DistributorApplyResponse distributorApplyDetail(Long userId) throws ProException {
		DistributorApplyResponse response = distributorFarmMapper.distributorApplyDetail(userId);
		if (response == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return response;
	}

	@Override
	public DistributorApplyResponse distributorApply(DistributorFarm request) throws ProException {
		DistributorApplyResponse response = distributorFarmMapper.distributorApplyDetail(request.getAdminUserId());
		if (response != null) {
			return response;
		}
		if (distributorFarmMapper.insertSelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "申请合伙人失败");
		}
		return distributorApplyDetail(request.getAdminUserId());
	}

	@Override
	public DistributorApplyResponse distributorApplyUpdate(DistributorFarm request) throws ProException {
		if (distributorFarmMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "修改合伙人信息失败");
		}
		return distributorApplyDetail(request.getAdminUserId());
	}

	@Override
	public DistributorFarmResponse distributorFarmDetail(Long userId) throws ProException {
		DistributorFarmResponse detail = distributorFarmMapper.selectByAdminUserId(userId);
		if (detail == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return detail;
	}

	@Override
	@Transactional
	public void updatedDistributor(DistributorUpdateRequest request) throws ProAffairException {
		DistributorFarm farm = BeanMapper.map(request, DistributorFarm.class);
		if (distributorFarmMapper.updateByPrimaryKeySelective(farm) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
}
